<?php
function accm_Category_Management(){ 
    
    global $wpdb, $user_ID, $msg;
    
    ?>
<div class="wrap" id="accm_Site_Manager">        
        <h2>Administration Panel - Categories</h2>
        <div id="tab-menu">
            <ul>
                <li class="right-tab <?php print ($_GET['tabmenu']=='category'? 'selected':'');?>"><a href="?page=<?php print $_GET['page'];?>&tabmenu=category">Categories</a></li>
                <!-- <li class="<?php print ($_GET['tabmenu']=='neighborhood'? 'selected':'');?>"><a href="?page=<?php print $_GET['page'];?>&tabmenu=neighborhood">Neighborhood</a></li>
                <li class="<?php print ($_GET['tabmenu']=='hotelregion'? 'selected':'');?>"><a href="?page=<?php print $_GET['page'];?>&tabmenu=hotelregion">Hotel Region</a></li>
                <li class="<?php print ($_GET['tabmenu']=='cuisine'? 'selected':'');?>"><a href="?page=<?php print $_GET['page'];?>&tabmenu=cuisine">Cuisine</a></li>
                <li class="<?php print ($_GET['tabmenu']=='pricerange'? 'selected':'');?>"><a href="?page=<?php print $_GET['page'];?>&tabmenu=pricerange">Price Range</a></li>-->
                <li class="<?php print ($_GET['tabmenu']=='categoryfield'? 'selected':'');?>"><strong>Category Field</strong></li>
                <li class="<?php print ($_GET['tabmenu']=='fields'? 'selected':'');?>"><a href="?page=<?php print $_GET['page'];?>&tabmenu=fields">Content Fields</a></li>
            </ul>
        </div> 
        <div style="clear: both; height: 30px; width: 100%;"></div>
    <form action="" method="post" id="frmCompany"  name="frmCompany"> 	 	 	 	 	
        <div id="table-company">            
            <div class="header row">
                <div class="col1 col"><input type="checkbox" id="chk_all" name="chk_all" /></div>
                <div class="col2 col">#</div>
                <div class="col3 col">Name</div>
                <div class="col5 col">Group</div>
                <div class="col5 col">Order</div>
                <div class="col5 col">Approved</div>                 
                <div class="col7 col">SubCategory</div>              
                <div class="col7 col">Edit</div>
                <div class="col7 col">Fields</div>
            </div>
            <?php
            $table = $wpdb->prefix.'category_taxonomy';
            $i = 1;
            if($_POST['btnSubmit']){
                $title = addslashes($_POST['FieldLabel']);
                $type = $_GET['tabmenu'];          
                $approved = ($_REQUEST['approved'])? 1 : 0;     
                if($_POST['hidID']<=0)
                    $sql = "INSERT INTO ". $table.
                            " SET title='$title' ,type='$type' , approved =".$approved.' , sort='.$_POST['FieldOrder'];
                else 
                     $sql = "UPDATE ". $table. 
                                " SET title='$title' ,type='$type' , approved =".$approved." , sort=".$_POST['FieldOrder'].
                            " WHERE id=".$_POST['hidID'];            
                $wpdb->query($sql);  
                print '<script type="text/javascript"> window.location = "?page=accm_Site_Manager&tabmenu='.$_GET['tabmenu'].'" </script>';             
            }
            if($_POST['btnDelete']){                
              $sql = "UPDATE ". $table." 
                        SET approved = 0, status = 1 
                        WHERE id in (".implode(',',$_POST['chk_item']).")";
               $wpdb->query($sql);  
               print '<script type="text/javascript"> window.location = "?page=accm_Site_Manager&tabmenu='.$_GET['tabmenu'].'" </script>'; 
            }
            //approve or denied
            if($_POST['btnApprove'] || $_POST['btnDeny']){   
                $approved = 1;
                if ($_POST['btnDeny']) $approved = 0;
                $sql = "UPDATE ". $table." 
                        SET approved = ".$approved." WHERE id in (".implode(',',$_POST['chk_item']).")";
                $wpdb->query($sql);  
                print '<script type="text/javascript"> window.location = "?page=accm_Site_Manager&tabmenu='.$_GET['tabmenu'].'" </script>'; 
            }
            //---------------------------------------------------
            $sql = "SELECT *, 1 as approved FROM  ".$wpdb->prefix."terms terms
                    INNER JOIN ".$wpdb->prefix."term_taxonomy termx ON termx.term_id = terms.term_id
                    WHERE termx.taxonomy in ('bs_categery','neighborhood','hotelregion','cuisine','pricerange') 
                    ORDER BY termx.taxonomy";
            $db_terms = $wpdb->get_results($sql);
           
            foreach($db_terms as $term) { ?>
            <div class="row">
                <div class="col1 col"><input class="chk_item" type="checkbox" id="chk_item<?php print $term->term_id;?>" name="chk_item[]" value="<?php print $term->term_id; ?>" /></div>
                <div class="col2 col"><?php print $term->term_id;?></div>
                <div class="col3 col"><?php print $term->title;?></div>
                <div class="col5 col">Group</div>
                <div class="col5 col"><?php print $term->sort;?></div>
                <div class="col5 col"><?php print $term->approved==1?'Yes':'No';?></div>                 
                <div class="col7 col"><a href="?page=accm_Site_Manager&tabmenu=subcategory&cat_id=<?php print $term->term_id; ?>">View</a></div>          
                <div class="col7 col"><a href="?page=accm_Site_Manager&tabmenu=category&act=update&id=<?php print $term->term_id; ?>#field">Edit</a></div>
                <div class="col7 col"><a href="?page=accm_Site_Manager&tabmenu=categoryfield&act=field&cat_id=<?php print $term->term_id; ?>#field">Edit</a></div>              
            </div>
             <?php } ?>
        </div>
        <div class="footer">           
            <input type="button" value="Add Category" id="btnAddCategory" name="btnAddCategory" class="button">
            <input type="submit" value="Approve" id="btnApprove" name="btnApprove" class="button">
            <input type="submit" value="Deny" id="btnDeny" name="btnDeny" class="button">
            <input type="submit" value="Delete" id="btnDelete" name="btnDelete" class="button">
        </div>
    </form> 
     <a name="field"></a>
<?php
       //update commnay       
       if(($_GET['act']=='update' && ($_GET['id'])) || $_GET['act']=='add') {
        
            $sql = "SELECT * FROM ". $table." WHERE id=" . $_GET['id'] ;
            $db = $wpdb->get_row($sql);
            if($db) {
                $FieldLabel = stripcslashes(trim($db->title));
                $FieldKey = trim($db->type);   
                $FieldOrder = trim($db->sort);    
                $FieldApp =  $db->approved;  
            }else
                $FieldKey =  $_GET['tabmenu'];
        ?>     
    <br clear=all /> 
    <h2>AddNew/Update Field</h2>
    <div class="wrap" id="register_user" style="width: 400px">     
        <form enctype="multipart/form-data" action="" method="post" name="frmField" id="frmField">	
            <table width="100%" cellspacing="5" cellpadding="0" border="0">
            <tbody><tr>
            	<td valign="top" align="left" colspan="2"> &nbsp;           	
               	</td>
            </tr>   
            
            <tr valign="top" width="100%"><td align="right" colspan="2"><i><font color="#bd0000">*</font> Indicates Required Information</i></td></tr>        
    		<tr><td colspan="2" class="formheader1">Field Information</td></tr>
            <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> Name:</label></td><td align="left" style="width:72%;">        	
                    	<input size="40" type="text" class="required" value="<?php print $FieldLabel;?>" name="FieldLabel" id="FieldLabel">                
                </td></tr>        
            <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> Field Key:</label></td><td align="left" style="width:72%;">        	
                    	<input size="40"  type="text" class="required" value="<?php print $FieldKey;?>" name="FieldKey" id="FieldKey">                
            	</td></tr>
              <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> Order:</label></td><td align="left" style="width:72%;">        	
                    	<input type="text" value="<?php print $FieldOrder;?>" name="FieldOrder" id="FieldOrder">                
            	</td></tr>
             <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> Approve:</label></td><td align="left" style="width:72%;">        	
                    	<input type="checkbox" <?php print ($FieldApp? 'checked=true' : '') ;?> value="1" name="approved" id="approved">                
            	</td></tr>
             <tr valign="top" width="100%"><td valign="top" align="right" colspan="2">
             <input type="hidden" value="<?php print $_GET['id'];?>" id="hidID" name="hidID" />
			<input type="button" id="btnReset" name="btnResett" value="Reset"  class="button">&nbsp;&nbsp;<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit"  class="button">
            </td></tr>        
        </tbody></table>
        </form>
        
   </div>
</div>
<?php } ?>
 <script type="text/javascript">
        (function($){
            $(document).ready(function() {                
                 $('#btnAddCategory').click(function(){                        
                        window.location = "?page=<?php print $_GET['page'];?>&tabmenu=<?php print $_GET['tabmenu'];?>&act=add#field";
                 });  
            }); 
            $('#chk_all').click(function(){               
                if (this.checked) 
                    $('.chk_item').attr('checked',true);
                else
                   $('.chk_item').removeAttr('checked'); 
            });  
            $('#btnDelete').click(function(){
                 chk = false;
                 $('.chk_item').each(function(){
                    if(this.checked) chk = true;
                 });
                 if (!chk) {
                    alert('Please choose item to delete!');
                    return false;
                 } else {
                    return confirm('Are you sure detele!');
                 }
            }); 
            $('#btnApprove').click(function(){
                 chk = false;
                 $('.chk_item').each(function(){
                    if(this.checked) chk = true;
                 });
                 if (!chk) {
                    alert('Please choose item to delete!');
                    return false;
                 } else {
                    return true;
                 }
            }); 
            $('#btnDeny').click(function(){
                 chk = false;
                 $('.chk_item').each(function(){
                    if(this.checked) chk = true;
                 });
                 if (!chk) {
                    alert('Please choose item to delete!');
                    return false;
                 } else {
                    return true;
                 }
            }); 
        })(jQuery);
     </script>     
<?php } ?> 